1 /*
2 * Angkor Web Framework
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */
7
8 package com.tirsen.angkor;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12
13 import javax.servlet.ServletException;
14 import javax.servlet.http.HttpSession;
15 import java.io.IOException;
16
17 /***
18 * TODO document ApplicationFactory
19 *
20 * <!-- $Id: ApplicationFactory.java,v 1.3 2002/10/13 19:59:22 tirsen Exp $ -->
21 * <!-- $Author: tirsen $ -->
22 *
23 * @author Jon Tirs´n (tirsen@users.sourceforge.net)
24 * @version $Revision: 1.3 $
25 */
26 public class ApplicationFactory
27 {
28 private static Log logger = LogFactory.getLog(ApplicationFactory.class);
29
30 private Class applicationClass;
31 private HttpSession session;
32
33 public ApplicationFactory(HttpSession session)
34 {
35 this.session = session;
36 }
37
38 public Application getApplication()
39 {
40 Application application = null;
41 try
42 {
43 application = (Application) getSessionAttribute(getApplicationSessionAttributeName());
44 }
45 catch (Exception e)
46 {
47 throw new RuntimeException("Failed to access application:" + e.getMessage());
48 }
49 if (application == null)
50 {
51 throw new IllegalStateException("Application has not been initialized for this request you need to deploy the ApplicationFilter correctly.");
52 }
53 return application;
54 }
55
56 private String getApplicationSessionAttributeName()
57 {
58 //return getApplicationPath() + "/application";
59 // TODO
60 return "application";
61 }
62
63 public void ensureApplicationInit(Class applicationClass) throws IOException, ServletException
64 {
65 Application application = (Application) getSessionAttribute(getApplicationSessionAttributeName());
66 if (application == null)
67 {
68 try
69 {
70 application = (Application) applicationClass.newInstance();
71 }
72 catch (Exception e)
73 {
74 logger.error("could not instantiate application");
75 throw new ServletException("could not instantiate application: " + e.getMessage());
76 }
77 setApplication(application);
78 }
79 }
80
81 public void setApplication(Application application)
82 {
83 setSessionAttribute(getApplicationSessionAttributeName(), application);
84 }
85
86 protected Object getSessionAttribute(String attribute)
87 {
88 return session.getAttribute(attribute);
89 }
90
91 protected void setSessionAttribute(String attribute, Object value)
92 {
93 session.setAttribute(attribute, value);
94 }
95 }
This page was automatically generated by Maven